package Testing;

import static org.testng.AssertJUnit.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.AssertJUnit;
import com.thoughtworks.selenium.*;

public class Google {
	private static Selenium selenium;
	public String url = "http://172.16.1.88:8431/";
	public String[] Headers = null;
	/*public Object[][] a = new Object[5][];*/

	/*
	 * @Test public void testsLogin() { System.out.println("Executing"); }
	 */
	@BeforeClass
	public void startSelenium() {
		selenium = new DefaultSelenium("localhost", 4444,
				"*chrome C:\\Program Files\\Mozilla Firefox\\firefox.exe", url);
		// "c:\\Program Files\\Mozilla Firefox\\firefox.exe",\
		selenium.start();
		/* selenium.open("http://www.google.com"); */
		selenium.open(url);
		selenium.windowFocus();
		selenium.windowMaximize();

	
	}

	@DataProvider(name = "DP2")
	/* public ArrayList<Object> createData2() { */
	public Object[][] createData2() {
		/*
		 * {{"INDIA","Karnataka","Bangalore",
		 * "Ingram micro india ltd.","FY 2011","FY 2012"}};
		 */
		ArrayList<Object> retObjArr = null;
		tableArray tA = new tableArray();

		retObjArr = tA.getTableArray("C:\\Users\\mkasim\\Desktop\\Login.xls",
				"Sheet1", "Sales by Product");
		Headers = (String[]) retObjArr.get(0);
		Object[][] Data = (Object[][]) retObjArr.get(1);
		Concatenate cc = new Concatenate();
		Object[][] DData = null;
		DData = cc.concat(Data);
		return (DData);
	}

	@DataProvider(name = "DP1")
	public Object[][] createData1() {

		ArrayList<Object> retObjArr = null;
		tableArray tA = new tableArray();
		retObjArr = tA.getTableArray("C:\\Users\\mkasim\\Desktop\\Login.xls",
				"Sheet1", "Login");
		Headers = (String[]) retObjArr.get(0);
		
		/* System.out.println("UserName " + Headers[0]); */
		Object[][] Data = (Object[][]) retObjArr.get(1);
		/*
		 * for (int i = 0; i < Data.length; i++) { for (int j = 0; j <
		 * Data[i].length; j++) { System.out.println("Data[" + i + "][" + j +
		 * "]" + Data[i][j]); } } System.out.println(Data.length);
		 */Concatenate cc = new Concatenate();
		Object[][] DData = null;
		DData = cc.concat(Data);
		/*
		 * for (int j = 0; j < 1; j++) { for (int i = 0; i < DData.length; i++)
		 * { System.out.println("Google " + DData[i][j]); } }
		 */return DData;
	}

	@Test(dataProvider = "DP1")
	/* public void testLogin(Object[][] a,Object[][] b) throws Exception { */
	public void testLogin(String Data) throws Exception {
		/* public void testLogin(String a) throws Exception { */

		Concatenate ct = new Concatenate();
		String[] Data1 = ct.split(Data);

		selenium.open("/");
		selenium.windowFocus();
		selenium.windowMaximize();

		for (int i = 0; i < Headers.length; i++) {
			System.out.println("Headers[i]"+ Headers[i]);	
			}
			
			Google.actions(Data1, Headers);

		
		/*
		 * System.out.println("login_form_email_address "+ Headers[0] +
		 * "a = "+a);
		 */
		/* selenium.type("id=login_form_password", passWord); */
		/*
		 * selenium.type("id="+Headers[0], Data1[0]);
		 * selenium.type("id="+Headers[1], Data1[1]);
		 */

		/*
		 * for (int i = 0; i < Headers.length; i++) { selenium.type("id=" +
		 * Headers[i], Data1[i]); }
		 */
		
		/*System.out.println("Headers[0]"+ Headers[0]);*/	
		
		
		/*selenium.click("id=login_form_submit");
		selenium.waitForPageToLoad("100000");
		*/
		AssertJUnit.assertEquals(selenium.getTitle(), "Zyme | Home |");
		/*
		 * selenium.click("id=logout"); selenium.waitForPageToLoad("100000");
		 */
	}

	/*
	 * public static void actions(String[] data1, String[] headers2) throws
	 * Exception { Links lk = new Links(); isNumber in = new isNumber();
	 * HashMap<String, String> hm = lk.links(); HashMap<String, Integer> links =
	 * new HashMap<String, Integer>(); links.put("dropdownbox", 1);
	 * links.put("radiobutton", 2); links.put("link", 3); links.put("textbox",
	 * 4);
	 * 
	 * for (int i = 0; i < headers2.length; i++) {
	 * System.out.println(headers2[i] + "=" + hm.get(headers2[i])); if
	 * (in.isNumber(headers2[i]) == false) { int link =
	 * links.get(hm.get(headers2[i])); System.out.println("link ===" + link);
	 * switch (link) { case 1: selenium.select(headers2[i], "label=" +
	 * data1[i]); break; case 2: selenium.type("id=" + headers2[i], data1[i]);
	 * break; case 3: selenium.click(headers2[i]);
	 * selenium.click("link=Reports"); break; System.out.println("case link ==="
	 * + link); selenium.waitForPageToLoad("10000");
	 * selenium.click("link=Reports"); selenium.click(headers2[i]); case 4:
	 * 
	 * selenium.type("id=" + headers2[i], data1[i]);
	 * System.out.println(headers2[i]); break; } } else {
	 * System.out.println("wait time"+ headers2[i]);
	 * selenium.waitForPageToLoad(headers2[i]); } } }
	 */
	public static void actions(String[] data1, String[] headers2)
			throws Exception {
		Links lk = new Links();
		isNumber in = new isNumber();
		HashMap<String, String> hm = lk.links();
		HashMap<String, Integer> links = new HashMap<String, Integer>();
		links.put("dropdownbox", 1);
		links.put("radiobutton", 2);
		links.put("link", 3);
		links.put("textbox", 4);
		
		
		
		for (int i = 0; i < headers2.length; i++) {
			System.out.println("headers2["+i+"]"+headers2[i]);
			/*System.out.println("hm.get(headers2[i])"+ hm.get(headers2[i]).split(";")[0]);
			System.out.println(headers2[i] + "=" + hm.get(headers2[i]));
			*/	
/*			System.out.println("headers2["+i+"]"+headers2[i]);
			System.out.println("headers2[i].split"+headers2[i].split(";")[0]);
			System.out.println("hm.get(headers2[i].split"+hm.get(headers2[i].split(";")[0]));
*/			
			
			int link = links.get(hm.get(headers2[i]).split(";")[0]);

			/*	System.out.println("link ===" + link);
			*/	switch (link) {
				case 1:
					selenium.select(headers2[i], "label=" + data1[i]);
					break;
				case 2:
					System.out.println("headers2["+i+"] =" + headers2[i]);
					selenium.click(headers2[i]);
		
					/*selenium.type("id=" + hm.get(headers2[i]).split(";")[0], data1[i]);*/
					break;
				case 3:
					selenium.click(headers2[i]);
					if(hm.get(headers2[i]).split(";")[1]!=null)
					{
					selenium.waitForPageToLoad(hm.get(headers2[i]).split(";")[1]);
					}
					/* selenium.click("link=Reports"); */
					break;
				case 4:

					selenium.type("id=" + headers2[i], data1[i]);
			/*		System.out.println(headers2[i]);
			*/		break;
			}
		}
	}

	/*
	 * public Object[][] createData() { Object[][] retObjArr1 =
	 * {{"logitech.zymenet@zymesolutions.com", "password"},
	 * {"logitech1.zymenet@zymesolutions.com", "password1"}}; Object[][]
	 * retObjArr2=null; List<Map<String,String>> retObjArr =null; Object[][]
	 * Data =null; tableArray tA = new tableArray(); retObjArr =
	 * tA.getTableArray("C:\\Users\\mkasim\\Desktop\\Login.xls", "Sheet1",
	 * "Login");
	 * 
	 * String [] Headers =
	 * retObjArr.get(0).keySet().toString().replace("[","").replace
	 * ("]","").split(",");
	 * System.out.println("retObjArr.get(0).keySet()"+retObjArr
	 * .get(0).keySet()); for(int i=0;i<Headers.length;i++) {
	 * Headers[i]=Headers[i].trim(); }
	 * 
	 * Data = new Object [ retObjArr.size() ] [ ] ;
	 * 
	 * for(int i=0;i<retObjArr.size();i++) { Data[ i ] = new
	 * Object[Headers.length]; for(int j=0;j<Headers.length;j++) {
	 * System.out.println
	 * ("retObjArr.get(i).get(Headers[j])"+retObjArr.get(i).get(Headers[j]));
	 * Data[i][j]=retObjArr.get(i).get(Headers[j]);
	 * System.out.println("Data["+i+"]["+j+"]"+Data[i][j]); } }
	 * 
	 * for (int i=0;i<a.length;i++) { System.out.println("a[i]"+a[i]); }
	 * System.out.println("retObjArr.toString()"+retObjArr.toString());
	 * System.out.println(retObjArr.size()); return (Data); }
	 */

	@Test(dataProvider = "DP2")
	public void testReportCreation(String Data) throws Exception {
		Concatenate ct = new Concatenate();
		String[] Data1 = ct.split(Data);
		Google.actions(Data1, Headers);
		/*
		 * selenium.click("link=Reports"); selenium.waitForPageToLoad("30000");
		 * selenium.click("link=New Report - Sales by Product");
		 * selenium.waitForPageToLoad("100000"); selenium.select("country",
		 * "label=" + Data1[0]); selenium.select("statename", "label=" +
		 * Data1[1]); selenium.select("cityname", "label=" + Data1[2]);
		 * selenium.select("partner", "label=" + Data1[3]);
		 * selenium.click("period"); selenium.click("time");
		 * selenium.select("startyear", "label=" + Data1[4]);
		 * selenium.select("endyear", "label=" + Data1[5]);
		 */
		/*selenium.type("reportname", Data1[0] + Data1[1] + Data1[2] + Data1[3]
				+ Data1[4] + Data1[5]);
		selenium.click("create");
		selenium.waitForPageToLoad("10000");
*/
		/*assertTrue(selenium.isTextPresent("Report " + Data1[0] + Data1[1]
				+ Data1[2] + Data1[3] + Data1[4] + Data1[5]
				+ " created successfully."));
		*/
		/*assertTrue(selenium.isTextPresent("Report " + "INDIAKarnatakaBangaloreIngram micro india ltd.FY 2011FY 2012"
+ " created successfully."));
		*/
		/*selenium.click("link=Reports");
		selenium.waitForPageToLoad("30000");
		*//*selenium.click("//table[@id='distributor_search_result']/thead/tr/th[2]/a/img");
		selenium.waitForPageToLoad("30000");
		*/
		
		/*selenium.click("link=Delete");*/
		assertTrue(selenium.getConfirmation().matches("^Are you sure, you want to delete INDIAKarnatakaBangaloreIngram micro india ltd\\.FY 2011FY 2012[\\s\\S]$"));
		selenium.waitForPageToLoad("30000");
/*		selenium.click("logout");
		sleepstate("Zyme");
*/	
		}

	@AfterClass(alwaysRun = true)
	public void stopSelenium() {
		this.selenium.stop();
	}

	public void sleepstate(String state) {
		int count = 1;
		while (count < 50) {
			String titlestate = selenium.getTitle();
			if (titlestate.equals(state)) {
			} else {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			count = count + 1;
		}
	}
}
